/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * AdminFilter.java
 *
 * 权限认证的Filter。这个针对注册管理员
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sundy.core.Context;
import com.sundy.core.Utility;

public class AdminFilter implements Filter {

	public AdminFilter() {
	}

	public void destroy() {

	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		Context context = Utility.obtainContext(request);
		if (context.getCurrentUser() == null
				|| context.getCurrentUser().getAdmin() != 1) {
			Utility.showError(request, response, "该功能仅限于管理员使用！");
			return;
		}
		chain.doFilter(req, resp);
	}

	public void init(FilterConfig arg0) throws ServletException {
	}

}
